<!DOCTYPE html>
<html lang="zh">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>10 demo</title>
</head>

<body>
    <h1>节流</h1>
    <h1>节流</h1>
    <h1>节流</h1>
    <h1>节流</h1>
    <h1>节流</h1>
    <h1>节流</h1>
    <h1>节流</h1>
    <h1>节流</h1>
    <h1>节流</h1>
    <h1>节流</h1>
    <h1>节流</h1>
    <h1>节流</h1>
    <h1>节流</h1>
    <h1>节流</h1>
    <h1>节流</h1>
    <h1>节流</h1>
    <h1>节流</h1>
    <h1>节流</h1>
    <h1>节流</h1>
    <h1>节流</h1>
    <h1>节流</h1>
    <h1>节流</h1>
    <h1>节流</h1>
    <h1>节流</h1>
    <script>
        function throttle(fn, delay) {
            let valid = true
            return function () {
                if (!valid) {
                    return false
                }
                valid = false;
                setTimeout(function () {
                    fn()
                    valid = true;
                }, delay)
            }
        }

        function showTop() {
            var scrollTop = document.documentElement.scrollTop;
            console.log('滚动条位置：' + scrollTop);
        }

        window.onscroll = throttle(showTop, 3000)
    </script>
</body>

</html>